package com.stockz.equity.charting.view.widget;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stockz.equity.charting.event.EventPublisher;
import com.stockz.equity.charting.event.RepaintStockChartPanelEvent;
import com.stockz.equity.charting.view.StockChartPanelState;
import com.stockz.equity.charting.view.StockChartPanelStructure;
import com.stockz.equity.charting.view.StockChartView;
import com.stockz.equity.charting.view.toolbar.ViewToolbar;
import com.stockz.equity.pattern.Child;

@Component
public class ZoomInButton extends JButton implements Child,ActionListener {
	private static final long serialVersionUID = -740570777039378639L;

	@SuppressWarnings("unchecked")
	private List<Class> classList = new ArrayList<Class>(1);
	@Autowired StockChartView stockChartView;
	
	public ZoomInButton() {
		//super(new ImageIcon("src/com/stockz/equity/resource/image/zoomin.jpg"));
		super(new ImageIcon(new ImageIcon("src/com/stockz/equity/resource/image/zoomin.jpg")
		.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH)));
		classList.add(ViewToolbar.class);
		addActionListener(this);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Collection<Class> getParents() {
		return classList;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		StockChartPanelState state = stockChartView.getSelectedStockChart()
		.getStockChartPanel().getStockChartPanelState();
		StockChartPanelStructure structure =stockChartView.getSelectedStockChart()
		.getStockChartPanel().getStockChartPanelStructure();
		int current = state.getTimeUnitSpace();
		int increment = structure.getZoomIncrement();
		if(current + increment < structure.getMaxZoom()){
			state.setTimeUnitSpace(current+increment);
			EventPublisher.publishEvent(
					new RepaintStockChartPanelEvent(
							stockChartView.getSelectedStockChart().getStockChartPanel()));
		}
	}
}
